import {observable,action} from 'mobx';
import {message} from 'antd';
import ajx from '../../utils/ajx';
import routerConf from '../../conf/routerConf';
import history from '../../utils/history';
import publicModel from '../../publicModel/publicModel';
import apiConf from '../../conf/apiConf';
import em from '../../utils/eventemitter';
import emKeys from '../../conf/eventemitterKeys';
const api = apiConf.file;
class Model {
    init = () => {
        this.getFileList();
    }
    @observable part = 'fileText';
    @observable dirNames = [];
    loginModal= null;
    @observable nameSpace = 'TEXT文件';
    @observable loginModalStat = false;
    @observable uploadParams = {};
    @observable fileList = [];
    @observable loading = true;
    @observable getFileListParams = {part:this.part,dir:this.dirNames.join('/'),nameSpace:this.nameSpace};

    @action goForward = (dirName) => {
        this.dirNames.push(dirName);
    }

    @action goBack = () => {
        this.dirNames.pop();
    }

    @action go = (dirName) => {
        const reg = new RegExp('/','ig');
        let str = this.dirNames.join(reg);
        str = str.split(dirName)[0];
        this.dirNames = str.split(reg);
    }

    @action setUploadParams = (data) => {
        this.uploadParams = data;
    }
    @action setLoginModalStat = (data) => {
        this.loginModalStat = data;
    }
    
    @action initGetFileListParams = () => {
        this.getFileListParams = {part:this.part,dir:this.dirNames.join('/'),nameSpace:this.nameSpace};
    }
    @action setGetFileListParams = (data) => {
        this.getFileListParams = {
            ...data,
            part:this.part
        }
    }
    
    @action getFileList = (data) => {
        this.loading = true;
        ajx.post({
            url:api.findList,
            data:this.getFileListParams
        }).then((res)=>{
            if (res && res.statusCode === 200) {
                this.fileList = res.data;
                this.loading = false;
            }
        }).catch(error=>{
            this.loading = false;
            if (error.status === 401) {
                message.warning('请登录');
                this.setLoginModalStat(true);
                if (this.loginModal) this.loginModal.refreshRandCode();
            }else{
                message.error('列表加载错误');
            }
            
        })
    }

    @action changeIndex = async (origin,target,dragIndex,hoverIndex) => {
        
        dragIndex = dragIndex+1;
        hoverIndex = hoverIndex+1;
        console.log({dragIndex,hoverIndex})
        await ajx.post({
            url:api.changeIndex,
            data:{origin,target,dragIndex,hoverIndex}
        }).then((res)=>{
            console.log(res);
            this.getFileList();
        }).catch((error)=>{
            console.log(error);
        });
    }

    @action deleteFile = async (_id) =>{
        await ajx.post({
            url:api.deleteById,
            data:{_id}
        }).then((res)=>{
            message.success('删除成功');
            this.getFileList();
        }).catch(error=>{
            message.error('删除失败');
        })
    }

    @action download = async (_id) => {
        await ajx.post({
            url:api.download,
            data:{_id}
        }).catch(error=>{
            message.error('下载失败');
        })
    }

    @action newFolder = async (dirName) => {
        await ajx.post({
            url:api.newFolder,
            data:{
                part:this.part,
                nameSpace:this.nameSpace,
                dir:this.dirNames.join('/'),
                dirName:dirName
            }
        }).then((res)=>{
            console.log(res)
            this.getFileList();
        }).catch(error=>{
            message.error('新建文件夹失败');
        })
    }

    @action openDir = async (record) =>{
        if (record.type==='dir' && record.isFile===0){
            this.dirNames.push(record.filename);
            em.emit(emKeys.changeTitle,['TEXT文件',...this.dirNames].join('/'));
            this.initGetFileListParams();
            console.log(this.getFileListParams)
            this.getFileList();
        }
    }

}

export default new Model();